# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=libuv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.46.0
pkgrel=1
pkgdesc="Cross-platform asynchronous I/O (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/libuv/libuv"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=(https://dist.libuv.org/dist/v${pkgver}/libuv-v${pkgver}.tar.gz{,.sign})
sha256sums=('111f83958b9fdc65f1489195d25f342b9f7a3e683140c60e62c00fbaccddddce'
            'SKIP')
# PGP key IDs are available from https://github.com/libuv/libuv/blob/v1.x/MAINTAINERS.md
validpgpkeys=(
  'CFBB9CA9A5BEAFD70E2B3C5A79A67C55A3679C8B' # Jameson Nash (@vtjnash)
  '612F0EAD9401622379DF4402F28C3C8DA33C03BE' # Santiago Gimeno
)

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-int-conversion"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SHARED_LIBRARY_NAME_WITH_VERSION=ON \
    "${_extra_config[@]}" \
    -DLIBUV_BUILD_TESTS=OFF \
    ../${_realname}-v${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check(){
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake -DLIBUV_BUILD_TESTS=ON ../${_realname}-v${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  # One these tests fails
  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -D -m644 "${srcdir}"/${_realname}-v${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
